<?php

class Zfarm_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        
    }

    public function indexAction()
    {
        // action body
    }
    public function dashboardAction(){
        
    }
    public function savefileAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        if(isset($_POST['path']) && isset($_POST['content'])){
            if(file_exists($_POST['path'])) {
                $fp = fopen($_POST['path'], "r+");
                var_dump(fwrite($fp, $_POST['content']));
                fclose($fp);
            }
        }
    }
    public function loadpageAction(){
        $this->_helper->layout->disableLayout();
        if(isset($_POST['file'])) {
	$file = $_POST['file'];
            if(file_exists($file)) {
                    $code = file_get_contents($file);
                    $this->view->code = $code;
                    $this->view->file = $file;
            }
        }
        
    }
    public function fileexplorerAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        function getDirectory( $path = '.', $level = 0 ){

            $ignore = array( 'cgi-bin', '.', '..', '.svn' );
            // Directories to ignore when listing output. Many hosts
            // will deny PHP access to the cgi-bin.
            $dh = @opendir( $path );
            // Open the directory to the handle $dh
            echo '<ul>';
            while( false !== ( $file = readdir( $dh ) ) ){
            // Loop through the directory

                if( !in_array( $file, $ignore ) ){
                // Check that this file is not to be ignored
                    // Just to add spacing to the list, to better
                    // show the directory tree.

                    if( is_dir( "$path/$file")){
                    echo '<li class="closed">';
                    // Its a directory, so we need to keep reading down...

                        echo "<span class='folder'>$file</span>";
                        getDirectory( "$path/$file", ($level+1) );
                        // Re-call this same function but on a new directory.
                        // this is what makes function recursive.

                    } else {
                            $extension = explode('.', $file);
                            $extension = end($extension);
                            echo "<li ><span class='file' id='file-$extension' path='$path/$file'>$file</span></li>";
                            // Just print out the filename
                    }

                }
            echo '</li>';
            }
            echo '</ul>';
            closedir( $dh );
            // Close the directory handle

        }
        getDirectory( "." ); 
    }
    public function extensionsAction(){
        //TODO:esse script ta meio estranho, poderia ficar melhor, depois eu corrijo
        $this->_helper->layout->disableLayout();
        $arquivos = array();
        $diretorio = "./application/modules/zfarm/extensions/";
        $ponteiro  = opendir($diretorio);
        
            while ($nome_itens = readdir($ponteiro)) {
                if (!is_dir($nome_itens)) { 
                    $filename = $diretorio.'/'.$nome_itens;
                    $handle  = fopen($filename, 'r');
                    $xmlString = fread ($handle, filesize ($filename));
                    $xml = new SimpleXMLElement($xmlString);
                    $category = (string)$xml->category;
                    $extension_name = str_replace(".xml", '', $nome_itens);
                    $arquivos[$category][] = array($extension_name, $xml->files->icon, $xml->name);
                    
                }
            }
            //var_dump($arquivos);
            $this->view->extensions = $arquivos;
        //$xmlString = fopen();
        //$xml = new SimpleXMLElement($xmlString);
    }
    public function codenavigatorAction(){
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        if(isset($_POST['file'])) {
	$file = $_POST['file'];
            if(file_exists($file)) {
                $code = file_get_contents($file);
                $path_info = pathinfo($file);
                $extension = $path_info['extension'];
                switch ($extension) {
                    case 'php':
                        $tokens = new ZFarm_Tokenizers_PHP();
                        break;
                    case 'phtml':
                        $tokens = new ZFarm_Tokenizers_PHP();
                        break;
                    case 'css':
                        $tokens = new ZFarm_Tokenizers_CSS();
                        break;
                    case 'js':
                        $tokens = new ZFarm_Tokenizers_JS();
                        break;
                    default:
                        $tokens = array();
                        break;
                }
                $out = $tokens->tokenizeString($code);
                //print_r($out);
                for($i=0;$i<count($out); $i++){
                    if($out[$i]['type'] == 'T_CONST'){
                        echo 'const-'.$out[$i+2]['content'].'<br>';
                    }
                    if($out[$i]['type'] == 'T_CLASS'){
                        echo 'class-'.$out[$i+2]['content'];
                        if($out[$i+4]['type'] == 'T_EXTENDS'){
                            echo ' extends-'.$out[$i+6]['content'].'<br>';
                        }else{
                            echo '<br>';
                        }
                    }
                    if($out[$i]['type'] == 'T_FUNCTION'){
                        if($out[$i-2]['type'] == 'T_STATIC'){
                            echo 'static ';
                            $encapPosition = 4;
                        }else{
                            $encapPosition = 2;
                        }
                        if($out[$i-$encapPosition]['type'] == 'T_PUBLIC'){
                                echo 'public ';
                            }
                            if($out[$i-$encapPosition]['type'] == 'T_PROTECTED'){
                                echo 'protected ';
                            }
                            if($out[$i-$encapPosition]['type'] == 'T_PRIVATE'){
                                echo 'private ';
                        }
                        echo 'function-'.$out[$i+2]['content'].'<br>';
                    }
                }
            }
        }
    }
}

